package com.pengpeng.sheepfoldmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pengpeng.common.constant.ProductConstant;
import com.pengpeng.sheepfoldmall.product.dao.AttrAttrgroupRelationDao;
import com.pengpeng.sheepfoldmall.product.dao.AttrDao;
import com.pengpeng.sheepfoldmall.product.entity.AttrAttrgroupRelationEntity;
import com.pengpeng.sheepfoldmall.product.entity.AttrEntity;
import com.pengpeng.sheepfoldmall.product.service.AttrService;
import com.pengpeng.sheepfoldmall.product.vo.AttrAttrRelationVo;
import com.pengpeng.sheepfoldmall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpeng.common.utils.PageUtils;
import com.pengpeng.common.utils.Query;

import com.pengpeng.sheepfoldmall.product.dao.AttrGroupDao;
import com.pengpeng.sheepfoldmall.product.entity.AttrGroupEntity;
import com.pengpeng.sheepfoldmall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrGroupDao attrGroupDao;

    @Resource
    private AttrAttrgroupRelationDao relationDao;

    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 分页查询属性分组
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

        //封装查询条件
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();

        //如果分类id为其他，则加上查询条件
        if (catelogId != 0){
            queryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
        }

        //若入参中有查询关键字，则要把关键字也加上
        String queryKey = (String) params.get("key");  //获取参数中的检索关键字
        if (!StringUtils.isEmpty(queryKey)){
            //and后是括号中的多个条件
            queryWrapper.and( (obj) ->{
                obj.eq(AttrGroupEntity::getAttrGroupId,queryKey)
                        //封装成 like %xxx%，还有likeLeft和likeRight方法，见名知意
                        .or()
                        .like(AttrGroupEntity::getAttrGroupName,queryKey);
            });
        }

        //new Query<>().getPage(params)：将查询参数提取并返回一个page对象
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        //分页工具类将MyBatis中的某些数据封装成前端需要的数据
        return new PageUtils(page);
    }

    /**
     * 移除属性与属性分组的关联关系(可能是批量删除,所以参数是一个数组)
     * @param vos
     */
    @Override
    public void deleteRelation(AttrAttrRelationVo[] vos) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        baseMapper.deleteBatchRelation(collect);
    }

    /**
     * 删除分组，并级联删除分组关联的属性关系
     * @param asList
     */
    @Override
    public void removeGroupAndRelation(List<Long> asList) {
        //先删除分组
        this.removeByIds(asList);

        relationDao.removeGroupAndRelation(asList);

        //再删除级联的属性关系
//        for (Long id : asList) {
//            relationDao.delete(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
//                    .in(AttrAttrgroupRelationEntity::getAttrGroupId,id));
//        }
    }

    /**
     * TODO:查询对应分类下的属性分组以及属性分组下的属性详情（发布商品设置基本参数时回显）
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupWithAttrsVo> result = null;
        //先根据前端传来的分类id查询到对应分类下的属性分组有哪些
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCatelogId, catelogId));
        //若查询出来的属性分组不为空
        if (!attrGroupEntities.isEmpty()){
            result = attrGroupEntities.stream().map(group -> {
                AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
                //先把属性分组的基本属性对拷
                BeanUtils.copyProperties(group,attrGroupWithAttrsVo);
                //查询分组下的所有id
                List<AttrEntity> attrs = attrService.getAttrRelation(group.getAttrGroupId());
                attrGroupWithAttrsVo.setAttrs(attrs);
                return attrGroupWithAttrsVo;
            }).collect(Collectors.toList());
        }
        return result;
    }
}